package com.hxgis.common.conare;

import com.hxgis.common.entity.RainstormFeatureInfo;
import com.hxgis.common.utils.CollectionUtils;

import java.util.*;

/**
 * 暴雨区域处理
 */
public class RainFeatureArea {

    private String araFlag;

    private Set<RainFeatureTime> rainFeatureTimes = new HashSet<RainFeatureTime>();

    public RainFeatureArea(RainFeatureTime rainFeatureTime) {
        rainFeatureTimes.add(rainFeatureTime);
        //生成区域标识
        araFlag = UUID.randomUUID().toString().replace("-","");
        rainFeatureTime.setAreaFlag(araFlag);
    }

    public void add(RainFeatureTime rainFeatureTime){
        rainFeatureTimes.add(rainFeatureTime);
        rainFeatureTime.setAreaFlag(araFlag);
    }

    public void merge(RainFeatureArea rainstormArea){
        rainFeatureTimes.addAll(rainstormArea.getRainstormTimes());
        setAraFlag(araFlag);
    }

    private void initAraFlag(){
        for (RainFeatureTime rainFeatureTime : rainFeatureTimes) {
            RainstormFeatureInfo rainstormFeatureInfo = rainFeatureTime.getRainstormInfos().get(0);
            String courseAreas = rainstormFeatureInfo.getCourseArea();
            if (courseAreas!=null)araFlag = courseAreas;
        }
        setAraFlag(araFlag);
        return;
    }

    public List<RainstormFeatureInfo> getRainstormInfoList(){
        initAraFlag();
        List<RainstormFeatureInfo> rainstormFeatureInfos = new ArrayList<RainstormFeatureInfo>();
        for (RainFeatureTime rainFeatureTime : rainFeatureTimes) {
            for (RainstormFeatureInfo rainstormFeatureInfo : rainFeatureTime.getRainstormInfos()) {
                rainstormFeatureInfo.setCourseArea(araFlag);
                rainstormFeatureInfos.add(rainstormFeatureInfo);
            }
        }
        return rainstormFeatureInfos;
    }

    //进行条件过滤
    public boolean check(RainFeatureTime rainFeatureTime){
        // 计算 时间是否在范围内
        List<RainFeatureTime> rainstormTimes = checkTime(rainFeatureTime);
        if (!CollectionUtils.isNotBlank(rainstormTimes)) {
            return false;
        }
        return true;
    }

    /**
     * 校验时间是否存在交集
     * @param rainFeatureTime
     * @return
     */
    private List<RainFeatureTime> checkTime(RainFeatureTime rainFeatureTime){
        List<RainFeatureTime> rainstormTimeList = new ArrayList<RainFeatureTime>();
        for (RainFeatureTime time : rainFeatureTimes) {
            if (time.checkTime(rainFeatureTime)){
                rainstormTimeList.add(time);
            }
        }
        return rainstormTimeList;
    }


    public Set<RainFeatureTime> getRainstormTimes() {
        return rainFeatureTimes;
    }

    public void setRainstormTimes(Set<RainFeatureTime> rainstormTimes) {
        this.rainFeatureTimes = rainstormTimes;
    }

    public String getAraFlag() {
        return araFlag;
    }

    public void setAraFlag(String araFlag) {
        this.araFlag = araFlag;
        for (RainFeatureTime rainFeatureTime : rainFeatureTimes) {
            rainFeatureTime.setAreaFlag(araFlag);
        }
    }
}
